﻿/*
 * 名称：TGuildManager
 * 创建人：John
 * 创建时间：2012-3-6 9:30:56
 * 描述: 行会管理类
 *********************************************
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SystemModule;

namespace M2Server
{
    public class TGuildManager
    {
        /// <summary>
        /// 游戏行会列表
        /// </summary>
        public IList<TGUild> GuildList = null;

        /// <summary>
        /// 新建行会
        /// </summary>
        /// <returns></returns>
        public bool AddGuild(string sGuildName, string sChief)
        {
            bool result = false;
            if (M2Share.CheckGuildName(sGuildName) && (FindGuild(sGuildName) == null))
            {
                TGUild Guild = new TGUild(sGuildName);
                Guild.SetGuildInfo(sChief);
                Guild.GuildMemberCount = M2Share.g_Config.nGuildMemberCount;
                GuildList.Add(Guild);
                SaveGuildList();
                result = true;
            }
            return result;
        }

        /// <summary>
        /// 删除行会
        /// </summary>
        /// <returns></returns>
        public bool DelGuild(string sGuildName)
        {
            bool result = false;
            TGUild Guild;
            for (int i = 0; i < GuildList.Count; i++)
            {
                if (GuildList.Count <= 0)
                {
                    break;
                }
                Guild = GuildList[i];
                if (string.Compare(Guild.sGuildName, sGuildName, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    if (Guild.m_RankList.Count > 1)
                    {
                        break;
                    }
                    Guild.BackupGuildFile();
                    GuildList.RemoveAt(i);
                    SaveGuildList();
                    result = true;
                    break;
                }
            }
            return result;
        }

        public void ClearGuildInf()
        {
            if (GuildList.Count > 0)
            {
                for (var i = 0; i < GuildList.Count; i++)
                {
                    Dispose(GuildList[i]);
                }
            }
            GuildList.Clear();
        }

        public TGuildManager()
        {
            GuildList = new List<TGUild>();
        }

        ~TGuildManager()
        {
            Dispose(GuildList);
        }

        /// <summary>
        /// 查找行会
        /// </summary>
        /// <param name="sGuildName"></param>
        /// <returns></returns>
        public TGUild FindGuild(string sGuildName)
        {
            TGUild result = null;
            if (GuildList.Count > 0)
            {
                for (int i = 0; i < GuildList.Count; i++)
                {
                    if (string.Compare(GuildList[i].sGuildName, sGuildName, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        result = GuildList[i];
                        break;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 加载游戏行会
        /// </summary>
        public void LoadGuildInfo()
        {
            TStringList LoadList;
            TGUild Guild;
            string sGuildName;
            if (File.Exists(M2Share.g_Config.sGuildFile))
            {
                LoadList = new TStringList();
                LoadList.LoadFromFile(M2Share.g_Config.sGuildFile);
                if (LoadList.Count > 0)
                {
                    for (int i = 0; i < LoadList.Count; i++)
                    {
                        sGuildName = LoadList[i].Trim();
                        if (sGuildName != "")
                        {
                            Guild = new TGUild(sGuildName);
                            GuildList.Add(Guild);
                        }
                    }
                }
                Dispose(LoadList);
                for (int i = GuildList.Count - 1; i >= 0; i--)
                {
                    if (GuildList.Count <= 0)
                    {
                        break;
                    }
                    Guild = GuildList[i];
                    if (!Guild.LoadGuild())
                    {
                        M2Share.MainOutMessage(Guild.sGuildName + " 读取出错！！！");
                        Dispose(Guild);
                        GuildList.RemoveAt(i);
                        SaveGuildList();
                    }
                }
                M2Share.MainOutMessage("已读取 [" + GuildList.Count + "] 个行会信息...");
            }
            else
            {
                M2Share.MainOutMessage("行会信息文件未找到！！！");
            }
        }

        /// <summary>
        /// 取玩家所属的行会
        /// </summary>
        /// <returns></returns>
        public TGUild MemberOfGuild(string sName)
        {
            TGUild result = null;
            if (GuildList.Count > 0)
            {
                for (int i = 0; i < GuildList.Count; i++)
                {
                    if (GuildList[i].IsMember(sName))
                    {
                        result = GuildList[i];
                        break;
                    }
                }
            }
            return result;
        }

        public void SaveGuildList()
        {
            TStringList SaveList;
            if (M2Share.nServerIndex != 0)
            {
                return;
            }
            SaveList = new TStringList();
            if (GuildList.Count > 0)
            {
                for (int i = 0; i < GuildList.Count; i++)
                {
                    SaveList.Add(GuildList[i].sGuildName);
                }
            }
            try
            {
                SaveList.SaveToFile(M2Share.g_Config.sGuildFile);
            }
            catch
            {
                M2Share.MainOutMessage("行会信息保存失败！！！");
            }
            SaveList.Dispose();
            Dispose(SaveList);
        }

        public void Run()
        {
            TGUild Guild;
            bool boChanged;
            TWarGuild WarGuild;
            try
            {
                if (GuildList.Count > 0)
                {
                    for (int i = 0; i < GuildList.Count; i++)
                    {
                        Guild = GuildList[i];
                        boChanged = false;
                        for (int j = Guild.GuildWarList.Count - 1; j >= 0; j--)
                        {
                            if (Guild.GuildWarList.Count <= 0)
                            {
                                break;
                            }
                            WarGuild = Guild.GuildWarList[j];
                            if ((HUtil32.GetTickCount() - WarGuild.dwWarTick) > WarGuild.dwWarTime)
                            {
                                Guild.EndWar(WarGuild.Guild);
                                Guild.GuildWarList.RemoveAt(j);
                                Dispose(WarGuild);
                                boChanged = true;
                            }
                        }
                        if (boChanged)
                        {
                            Guild.UpdateGuildFile();
                        }
                        Guild.CheckSaveGuildFile();
                    }
                }
            }
            catch
            {
                M2Share.MainOutMessage("[异常] TGuildManager.Run");
            }
        }

        /// <summary>
        /// 释放指定对象资源
        /// </summary>
        /// <param name="obj"></param>
        public void Dispose(object obj)
        {
            obj = null;
        }
    }
}